package com.ssm.changeunit;

import com.ssm.changeunit.persistance.ChangeUnitItemDAO;
import com.ssm.changeunit.persistance.ChangeUnitItemDTO;
import com.ssm.changeunit.persistance.PersistanceException;

/**
 * 
 * @author 시리우스
 */
public class ChangeUnitItem extends AbstractProduct {
    private String changeUnitItemId;
    
    private ChangeUnitItemDAO changeUnitItemDAO;
    
    /**
     * 생성자
     * 
     * @param changeUnitItemId
     */
    public ChangeUnitItem(String changeUnitItemId) {
        this.changeUnitItemId = changeUnitItemId;
        this.changeUnitItemDAO = new ChangeUnitItemDAO();
    }
    
    /**
     * ChangeUnitItem을 가져옵니다.
     * 
     * @return
     */
    public String getChangeUnitItemId() {
        return changeUnitItemId;
    }
    
    /**
     * ChangeUnit을 가져옵니다.
     * 
     * @return
     */
    public ChangeUnit getChangeUnit() throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);
        
        try {
            return new ChangeUnitFactory().createProduct(changeUnitItemDTO.getChangeUnitId());
        }
        catch(CreateException ex) {
            throw new PersistanceException(ex);
        }
    }
    
    /**
     * ChangeUnit을 세팅합니다.
     * 
     * @param changeUnit
     */
    public void setChangeUnit(ChangeUnit changeUnit) throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);
        
        changeUnitItemDTO.setChangeUnitId(changeUnit.getChangeUnitId());
        
        changeUnitItemDAO.update(changeUnitItemDTO);
    }
    
    /**
     * File Path를 가져옵니다.
     * 
     * @return
     */
    public String getFilePath() throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);
        
        return changeUnitItemDTO.getFilePath();
    }
    
    /**
     * File Path를 세팅합니다.
     * @param filePath
     */
    public void setFilePath(String filePath) throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);
        
        changeUnitItemDTO.setFilePath(filePath);
        
        changeUnitItemDAO.update(changeUnitItemDTO);
    }
    
    /**
     * 첫번째 부모를 가져옵니다.
     * 
     * @return
     */
    public ChangeUnitItem getFirstParentChangeUnitItem() throws PersistanceException {
        try {
            ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);

            String firstParentChangeUnitItemId = changeUnitItemDTO.getFirstParentChangeUnitItemId();

            return new ChangeUnitItemFactory().createProduct(firstParentChangeUnitItemId);
        }
        catch(CreateException ex) {
            throw new PersistanceException(ex);
        }
    }
    
    /**
     * 첫번째 부모를 세팅합니다.
     * 
     * @param changeUnitItem
     */
    public void setFirstParentChangeUnitItem(ChangeUnitItem changeUnitItem) throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);

        changeUnitItemDTO.setFirstParentChangeUnitItemId(changeUnitItem.getChangeUnitItemId());

        changeUnitItemDAO.update(changeUnitItemDTO);
    }
    
    /**
     * 두번째 부모를 가져옵니다.
     * 
     * @return
     */
    public ChangeUnitItem getSecondParentChangeUnitItem() throws PersistanceException {
        try {
            ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);

            return new ChangeUnitItemFactory().createProduct(changeUnitItemDTO.getSecondParentChangeUnitItemId());
        }
        catch(CreateException ex) {
            throw new PersistanceException(ex);
        }
    }
    
    /**
     * 두번째 부모를 세팅합니다.
     * 
     * @param changeUnitItem
     */
    public void setSecondParentChangeUnitItem(ChangeUnitItem changeUnitItem) throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);

        changeUnitItemDTO.setSecondParentChangeUnitItemId(changeUnitItemId);

        changeUnitItemDAO.update(changeUnitItemDTO);
    }

    /**
     * ChangeLog를 가져옵니다.
     * 
     * @return
     * @throws com.ssm.changeunit.persistance.PersistanceException
     */
    public ChangeLog getRefChangeLog() throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);
        
        try {
            return new ChangeLogFactory().createProduct(changeUnitItemDTO.getRefChangeLogId());
        }
        catch(CreateException ex) {
            throw new PersistanceException(ex);
        }
    }
    
    /**
     * ChangeLog를 세팅합니다.
     * @param changeLog
     * @throws com.ssm.changeunit.persistance.PersistanceException
     */
    public void setRefChangeLog(ChangeLog changeLog) throws PersistanceException {
        ChangeUnitItemDTO changeUnitItemDTO = changeUnitItemDAO.find(changeUnitItemId);
        
        changeUnitItemDTO.setRefChangeLogId(changeLog.getChangeLogId());
        
        changeUnitItemDAO.update(changeUnitItemDTO);
    }
}
